package com.vst.orm.dao.hibernate.original;

import java.io.Serializable;
import java.util.List;

import com.vst.orm.search.SearchResult;
import com.vst.orm.search.flex.FlexSearch;
import com.vst.orm.search.flex.FlexSearchWrapper;

public class FlexDAOAdapter {
	private static long mockDelay = 0L;
	private GeneralDAO dao;

	public void setDAO(GeneralDAO dao) {
		this.dao = dao;
	}

	public Object fetch(Serializable id, String className) throws Exception {
		if (mockDelay != 0L) {
			Thread.sleep(mockDelay);
		}
		Class<?> klass;
		try {
			klass = Class.forName(className);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			throw e;
		}
		return this.dao.fetch(klass, id);
	}

	public List fetchAll(String className) throws Exception {
		if (mockDelay != 0L) {
			Thread.sleep(mockDelay);
		}
		Class<?> klass;
		try {
			klass = Class.forName(className);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			throw e;
		}
		return this.dao.fetchAll(klass);
	}

	public Object create(Object object) throws Exception {
		if (mockDelay != 0L) {
			Thread.sleep(mockDelay);
		}
		this.dao.create(object);
		return object;
	}

	public Object update(Object object) throws Exception {
		if (mockDelay != 0L) {
			Thread.sleep(mockDelay);
		}
		this.dao.update(object);
		return object;
	}

	public Object createOrUpdate(Object object) throws Exception {
		if (mockDelay != 0L) {
			Thread.sleep(mockDelay);
		}
		this.dao.createOrUpdate(object);
		return object;
	}

	public void deleteById(Serializable id, String className) throws Exception {
		if (mockDelay != 0L) {
			Thread.sleep(mockDelay);
		}
		Class<?> klass;
		try {
			klass = Class.forName(className);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			throw e;
		}
		this.dao.deleteById(klass, id);
	}

	public void deleteEntity(Object object) throws Exception {
		if (mockDelay != 0L) {
			Thread.sleep(mockDelay);
		}
		this.dao.deleteEntity(object);
	}

	public void deleteList(Object[] list) throws Exception {
		if (mockDelay != 0L) {
			Thread.sleep(mockDelay);
		}
		for (int i = 0; i < list.length; i++) {
			deleteEntity(list[i]);
		}
	}

	public void deleteListById(Serializable[] ids, String className) throws Exception {
		if (mockDelay != 0L) {
			Thread.sleep(mockDelay);
		}
		for (int i = 0; i < ids.length; i++) {
			deleteById(ids[i], className);
		}
	}

	public List search(FlexSearch flexSearch) throws Exception {
		if (mockDelay != 0L) {
			Thread.sleep(mockDelay);
		}
		return this.dao.search(new FlexSearchWrapper(flexSearch));
	}

	public int searchLength(FlexSearch flexSearch) throws Exception {
		if (mockDelay != 0L) {
			Thread.sleep(mockDelay);
		}
		return this.dao.count(new FlexSearchWrapper(flexSearch));
	}

	public SearchResult searchAndLength(FlexSearch flexSearch) throws Exception {
		if (mockDelay != 0L) {
			Thread.sleep(mockDelay);
		}
		return this.dao.searchAndCount(new FlexSearchWrapper(flexSearch));
	}

	public Object searchUnique(FlexSearch flexSearch) throws Exception {
		if (mockDelay != 0L) {
			Thread.sleep(mockDelay);
		}
		return this.dao.searchUnique(new FlexSearchWrapper(flexSearch));
	}
}
